package cn.iocoder.yudao.module.trade.controller.admin.orderpostconfig;

import cn.hutool.core.util.RandomUtil;
import cn.iocoder.yudao.module.trade.api.drainage.DrainageApi;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO;
import cn.iocoder.yudao.module.trade.service.order.handler.DrainageOrderHandler;
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.UUID;

@Tag(name = "管理后台 - 订单回传扣量配置")
@RestController
@RequestMapping("/trade/order-post-test")
public class OrderPostController {

    @Resource
    private DrainageOrderHandler drainageOrderHandler;
    @Resource
    private DrainageApi drainageApi;

    @PostMapping("/test")
    @Operation(summary = "测试")
    public void test(String code, int times) {
        for (int i = 0; i < times; i ++) {
            cacheBXM(code);
            drainageOrderHandler.afterOrderCreate(createTradeOrder(), new ArrayList<>());
            drainageOrderHandler.afterPayOrder(createTradeOrder(), new ArrayList<>());
        }
    }

    private void cacheBXM(String code) {
        String uuid = UUID.randomUUID().toString();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("drainageCode", code);
        jsonObject.put("bxm_id", uuid);
        jsonObject.put("requestId", uuid);
        jsonObject.put("creativeId", UUID.randomUUID().toString());
        drainageApi.cacheMemberDrainageFlag(1L, jsonObject);
    }

    private TradeOrderDO createTradeOrder() {
        TradeOrderDO tradeOrderDO = new TradeOrderDO();
        tradeOrderDO.setUserId(1L);
        tradeOrderDO.setId(RandomUtil.randomLong());
        tradeOrderDO.setPayOrderId(1L);
        tradeOrderDO.setPayPrice(1000);
        tradeOrderDO.setPayTime(LocalDateTime.now());
        return tradeOrderDO;
    }
}
